{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "built-tiffany",
   "metadata": {},
   "source": [
    "# The basis of pytorch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "leading-thousand",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "greek-landing",
   "metadata": {},
   "source": [
    "## Tensor\n",
    "### Create a new Tensor\n",
    "```python\n",
    "a = torch.tensor(<array>)\n",
    "```\n",
    "\n",
    "### Some special tensor\n",
    "\n",
    "#### Zeros\n",
    "```python \n",
    "torch.zeros(m,n)\n",
    "```\n",
    "return a tensor with all zeros\n",
    "\n",
    "#### Eyes\n",
    "```python\n",
    "torch.eye(n)\n",
    "```\n",
    "\n",
    "return a n dimensional idential matrix\n",
    "\n",
    "#### Others\n",
    "```python\n",
    "torch.rand(size) -> U(0,1)\n",
    "torch.randn(size) -> N(0,1)\n",
    "torch.arange(start,end,step)\n",
    "torch.linspace(start,end,n)\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "grave-situation",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = torch.full((2,2),1)\n",
    "b = torch.full((2,2),2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "reverse-electric",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[1, 2],\n",
       "         [1, 2]],\n",
       "\n",
       "        [[1, 2],\n",
       "         [1, 2]]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.stack((a,b),dim=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "natural-cleanup",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 1, 2, 2],\n",
       "        [1, 1, 2, 2]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.cat((a,b),dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "parallel-reverse",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 1],\n",
       "        [1, 1],\n",
       "        [2, 2],\n",
       "        [2, 2]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.cat((a,b),dim=0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
